package com.zzyl.nursing.controller;


import com.github.pagehelper.Page;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.utils.PageUtils;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.ReservationService;
import com.zzyl.nursing.vo.CountByTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/member/reservation")
public class ReservationController extends BaseController {
    @Autowired
    private ReservationService reservationService;

    @GetMapping("/cancelled-count")
    public R getCancelledReservationCount(){
        int i = reservationService.getCancelledReservationCount();
        return R.ok(i);
    }

    @GetMapping("/countByTime")
    public R<List<CountByTime>> getCountByTime(long time){
        List<CountByTime> countByTime = reservationService.getCountByTime(time);
        return R.ok(countByTime);
    }

    @PostMapping
    public AjaxResult add(@RequestBody Reservation reservation){
        int add = reservationService.add(reservation);
        return add>0?success("成功"):error("失败");
    }

    @GetMapping("/page")
    public Map<String, Object> list(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "status", defaultValue = "0") Integer status){
        PageUtils.startPage();
        List<Reservation> all = reservationService.findAll(status);
        // 强转为 Page，获取 total
        Page<Reservation> page = (Page<Reservation>) all;
        // 手动封装成你要的结构
        Map<String, Object> data = new HashMap<>();
        data.put("total", page.getTotal());
        data.put("rows", page.getResult());
        data.put("code", 200);
        data.put("msg", "请求成功");

        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "操作成功");
        result.put("data", data);
        return result;

    }

    @PutMapping("/{id}/cancel")
    public AjaxResult cancel(@PathVariable Long id){
        boolean cancel = reservationService.cancel(id);
        return cancel?success("取消成功"):error("取消失败");
    }

}
